﻿var prizeTypeJson;
var prizeJson;
var PrizeTypeJsons;
var EventMouldJson = null;
var GoldJson = null;
var PropJson = null;
var TimePropJson = null;
var GetPrizeNameJson; //下拉物品  选择附赠物品
var pageSize = 15;
$(document).ready(function () {

    //按照物品类型进行 查询
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/PrizeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                PrizeTypeJsons = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $.ajax({
        type: "post",
        data: { action: "GetPrizeName" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                GetPrizeNameJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })

    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/PrizeTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                prizeTypeJson = data.json;
                $("#seleByType").append(InitSelectByType());
                PageClickFunc($("#seleByType").val(), 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#seleByType").change(function () {
        InitPrizeList($("#seleByType").val(), 1)
    })


    $("#btnSel").click(function () {
        var txtState = $("#txtNameBySel").val();
        AddMouldState(txtState)
    });
    $("#btnAdd").click(function () {
        Edit(null);
    });

})
PageClickFunc = function (pageclickednumber) {
    InitPrizeList($("#seleByType").val(), pageclickednumber);
}

function InitPrizeList(typeID, pageclickednumber) {
    $.ajax({
        type: "post",
        data: { action: "GetListByTypeID", typeID: typeID, pageIndex: pageclickednumber, pageSize: pageSize },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                var pageCount = data.flag;
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblTypeName' id='" + v.PrizeTypeID + "'>";
                    tbody += v.TypeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblExchangeType' id='" + v.ExchangeType + "'>";
                    tbody += v.ExchangeTypeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrice' >";
                    tbody += v.Price;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrizeCount' >";
                    tbody += v.PrizeCount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += decodeURIComponent(v.Remark);
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblSortNum' >";
                    tbody += v.SortNum;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' >";
                    tbody += v.IsTaskCondition;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsShopShow' >";
                    tbody += v.IsShopShow;
                    tbody += "</label></td>";
                    tbody += "<td><img name='imgPath' src='" + v.Image + "' width='100' height='100'/></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'><input type='button' onclick='Give(this)' value='附赠物品'>"
                    if (v.PrizeTypeID == 7 || v.PrizeTypeID == 8 || v.PrizeTypeID == 9 || v.PrizeTypeID == 10)
                        tbody += "<input type='button' onclick='ParamsSetting(this)' value='参数设置'";
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}
//查询下拉框选择方法
function InitSelectByType() {
    var sel = "";
    $.each(PrizeTypeJsons, function (k, v) {
        if (k == 0) {
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
            InitPrizeList(v.ID, 1)
        }
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function AddParmsMould(contr, typeID, prizeID) {
    var id = $(contr).parent().parent().attr("id");
    var eventMouldID = $("#selEventMould").val();
    var eventMouldName = $("#selEventMould").find("option:selected").text();
    var gainPrizeID = $("#selGainPrize").val();
    var gainPrizeName = $("#selGainPrize").find("option:selected").text();
    var minValue = $("#txtMinValue").val();
    var maxValue = $("#txtMaxValue").val(); funcDayAstrict
    var floatRatio = $("#txtFloatRatio").val();
    var funcChance = $("#txtFuncChance").val();
    var funcDayAstrict = $("#txtFuncDayAstrict").val();
    var placePrizeID = $("#selPlacePrizeID").val();
    var placePrizeName = $("#selPlacePrizeID").find("option:selected").text();
    var placeTime = $("#txtPlaceTime").val();
    var param1 = $("#txtParam1").val();
    var param2 = $("#txtParam2").val();

    var data = contr == null ? { action: "Add", eventMouldID: eventMouldID, prizeID: prizeID, gainPrizeID: gainPrizeID, minValue: minValue, maxValue: maxValue, floatRatio: floatRatio,
        funcChance: funcChance, funcDayAstrict: funcDayAstrict, placePrizeID: placePrizeID, placeTime: placeTime, param1: param1, param2: param2
    } :
                              { action: "Update", id: id, eventMouldID: eventMouldID, prizeID: prizeID, gainPrizeID: gainPrizeID, minValue: minValue, maxValue: maxValue, floatRatio: floatRatio, funcChance: funcChance, funcDayAstrict: funcDayAstrict,
                                  placePrizeID: placePrizeID, placeTime: placeTime, param1: param1, param2: param2
                              };

    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/FuncPrizeParmsAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                $.dialog.tips("更新成功！", 1);
                var tbody = "";
                if (contr == null) {

                    tbody += "<tr id='" + data + "'>"
                    //事件道具
                    if (typeID == 7) {
                        tbody += "<td><lable name='lblEventMouldName' id='" + eventMouldID + "'>";
                        tbody += eventMouldName;
                        tbody += "</label></td>";
                    }
                    //货币宝箱
                    else if (typeID == 8) {
                        tbody += "<td><lable name='lblGainPrize' id='" + gainPrizeID + "'>";
                        tbody += gainPrizeName;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblMinValue'>";
                        tbody += minValue;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblMaxValue'>";
                        tbody += maxValue;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblFloatRatio'>";
                        tbody += floatRatio;
                        tbody += "</label></td>";
                    }
                    //道具宝箱
                    else if (typeID == 9) {
                        tbody += "<td><lable name='lblGainPrize' id='" + gainPrizeID + "'>";
                        tbody += gainPrizeName;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblFuncChance'>";
                        tbody += funcChance;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblFuncDayAstrict'>";
                        tbody += funcDayAstrict;
                        tbody += "</label></td>";
                    }
                    //限时增强配置
                    else if (typeID == 10) {
                        tbody += "<td><lable name='lblPlacePrize' id='" + placePrizeID + "'>";
                        tbody += placePrizeName;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblPlaceTime'>";
                        tbody += placeTime;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblParam1'>";
                        tbody += param1;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblParam2'>";
                        tbody += param2;
                        tbody += "</label></td>";
                    }

                    tbody += "<td>";
                    tbody += "<input type='button' onclick='EditParms(\"" + typeID + "\",\"" + prizeID + "\",this)' value='更新'><input type='button' onclick='DeleteParms(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>";
                    $("#tblPrizeParms").append(tbody)
                }
                else {
                    $(contr).parent().parent().find("lable[name=lblEventMouldName]").first().text(eventMouldName);
                    $(contr).parent().parent().find("lable[name=lblEventMouldName]").first().attr("id", eventMouldID);
                    $(contr).parent().parent().find("lable[name=lblGainPrize]").first().text(gainPrizeName);
                    $(contr).parent().parent().find("lable[name=lblGainPrize]").first().attr("id", gainPrizeID);
                    $(contr).parent().parent().find("lable[name=lblMinValue]").first().text(minValue);
                    $(contr).parent().parent().find("lable[name=lblMaxValue]").first().text(maxValue);
                    $(contr).parent().parent().find("lable[name=lblFloatRatio]").first().text(floatRatio);
                    $(contr).parent().parent().find("lable[name=lblFuncChance]").first().text(funcChance);
                    $(contr).parent().parent().find("lable[name=lblFuncDayAstrict]").first().text(funcDayAstrict);
                    $(contr).parent().parent().find("lable[name=lblPlacePrize]").first().attr("id", placePrizeID);
                    $(contr).parent().parent().find("lable[name=lblPlacePrize]").first().text(placePrizeName);
                    $(contr).parent().parent().find("lable[name=lblPlaceTime]").first().text(placeTime);
                    $(contr).parent().parent().find("lable[name=lblParam1]").first().text(param1);
                    $(contr).parent().parent().find("lable[name=lblParam2]").first().text(param2);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}

function DeleteParms(contr) {
    var id = $(contr).parent().parent().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/FuncPrizeParmsAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(contr).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}


//附赠物品
function Give(contr) {
    var prizeID = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var content = "";
    content = "<input type='button' id='btnAddParams' value='添加附赠物品' onclick='EditGivePrize(\"" + prizeID + "\")' /><table border='1' ><thead><tr><td>物品名称</td><td>数量</td></tr></thead><tbody id='tblGivePrize'>";

    $.ajax({
        type: "post",
        data: { action: "GetList", prizeID: prizeID },
        dataType: "json",
        url: "../../Ajax/PrizeGiveAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                var json = data.json;
                $.each(json, function (k, v) {
                    content += "<tr id='" + v.ID + "'>"
                    content += "<td><lable name='lblName' id='" + v.GivePrizeID + "'>"
                    content += v.Name;
                    content += "</label></td>";
                    content += "<td><lable name='lblCount'>"
                    content += v.PrizeCount;
                    content += "</label></td>";
                    content += "<td>";
                    content += "<input type='button' onclick='EditGivePrize(\"" + prizeID + "\",\"" + v.GivePrizeID + "\",this)' value='更新'><input type='button' onclick='DeleteGivePrize(this)' value='删除'>"
                    content += "</td>";
                    content += "</tr>";
                })
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    content += "</tbody></table>";

    $.dialog({
        title: "附赠物品",
        content: content,
        button: [
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function DeleteGivePrize(contr) {
    var id = $(contr).parent().parent().attr("id");

    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/PrizeGiveAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(contr).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function EditGivePrize(prizeID, givePrizeID, contr) {
    var count = $(contr).parent().parent().parent().find("lable[name=lblCount]").first().text();
    var content = "";
    content = "<table><tr><td>附赠物品：<select id='selGivePrize'>" + InitSelectPrizeName(givePrizeID) + "</select></td></tr>"
            + "<tr><td>附赠数量：：<input type='text' id='txtCount' value='" + count + "' /></td></tr></table>";

    $.dialog({
        title: "修改附赠物品",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddGivePrizeMould(contr, prizeID);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddGivePrizeMould(contr, prizeID) {
    var givePrizeID = $("#selGivePrize").val();
    var givePrizeName = $("#selGivePrize>option:selected").text()
    var count = $("#txtCount").val();
    var id = $(contr).parent().parent().attr("id");

    var data = contr == null ? { action: "Add", prizeID: prizeID, count: count, givePrizeID: givePrizeID} :
                               { action: "Update", id: id, prizeID: prizeID, count: count, givePrizeID: givePrizeID };

    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PrizeGiveAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                $.dialog.tips("更新成功！", 1);
                var tbody = "";
                if (contr == null) {
                    tbody += "<tr id='" + data + "'>"
                    tbody += "<td><lable name='lblName' id='" + givePrizeID + "'>"
                    tbody += givePrizeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCount'>"
                    tbody += count;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='EditGivePrize(\"" + prizeID + "\",\"" + givePrizeID + "\",this)' value='更新'><input type='button' onclick='DeleteGivePrize(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>";
                    $("#tblGivePrize").append(tbody)
                }
                else {
                    $(contr).parent().parent().find("lable[name=lblName]").first().attr("id", givePrizeID)
                    $(contr).parent().parent().find("lable[name=lblName]").first().text(givePrizeName);
                    $(contr).parent().parent().find("lable[name=lblCount]").first().text(count);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}

function InitSelectPrizeName(value) {
    var sel = "";
    $.each(GetPrizeNameJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value) {
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        }
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function EditParms(typeID, prizeID, contr) {
    var eventMouldID = $(contr).parent().parent().find("lable[name=lblEventMouldName]").first().attr("id");
    var eventMouldName = $(contr).parent().parent().find("lable[name=lblEventMouldName]").first().text();
    var gainPrizeID = $(contr).parent().parent().find("lable[name=lblGainPrize]").first().attr("id");
    var gainPrizeName = $(contr).parent().parent().find("lable[name=lblGainPrize]").first().text();
    var minValue = $(contr).parent().parent().find("lable[name=lblMinValue]").first().text();
    var maxValue = $(contr).parent().parent().find("lable[name=lblMaxValue]").first().text();
    var floatRatio = $(contr).parent().parent().find("lable[name=lblFloatRatio]").first().text();
    var funcChance = $(contr).parent().parent().find("lable[name=lblFuncChance]").first().text();
    var funcDayAstrict = $(contr).parent().parent().find("lable[name=lblFuncDayAstrict]").first().text();
    var placePrizeID = $(contr).parent().parent().find("lable[name=lblPlacePrize]").first().attr("id");
    var placePrizeName = $(contr).parent().parent().find("lable[name=lblPlacePrize]").first().text();
    var placeTime = $(contr).parent().parent().find("lable[name=lblPlaceTime]").first().text();
    var param1 = $(contr).parent().parent().find("lable[name=lblParam1]").first().text();
    var param2 = $(contr).parent().parent().find("lable[name=lblParam2]").first().text();

    var content = "";
    //事件道具
    if (typeID == 7) {
        if (EventMouldJson == null) {
            InitEventMouldJson();
        }
        content = "<table><tr><td>事件选择：<select id='selEventMould'>" + InitEventMouldSelect(eventMouldID, EventMouldJson) + "</select></td></tr></table>"
    }
    //货币宝箱
    else if (typeID == 8) {
        if (GoldJson == null) {
            InitGoldJson();
        }
        content = "<table><tr><td>宝箱类型：<select id='selGainPrize'>" + InitEventMouldSelect(gainPrizeID, GoldJson) + "</select></td></tr>"
                + "<tr><td>最小值：<input type='text' id='txtMinValue' value='" + minValue + "' /></td></tr>"
                + "<tr><td>最大值：<input type='text' id='txtMaxValue' value='" + maxValue + "' /></td></tr>"
                + "<tr><td>浮动比：<input type='text' id='txtFloatRatio' value='" + floatRatio + "' /></td></tr>"
                + "</table>"
    }
    //道具宝箱
    else if (typeID == 9) {
        if (PropJson == null) {
            InitPropJson();
        }
        content = "<table><tr><td>选择物品：<select id='selGainPrize'>" + InitEventMouldSelect(gainPrizeID, PropJson) + "</select></td></tr>"
                + "<tr><td>概率：<input type='text' id='txtFuncChance' value='" + funcChance + "' /></td></tr>"
                + "<tr><td>日限：<input type='text' id='txtFuncDayAstrict' value='" + funcDayAstrict + "' /></td></tr>"
                + "</table>"
    }
    //限时增强配置
    else if (typeID == 10) {
        if (TimePropJson == null) {
            InitTimePropJson();
        }
        content = "<table><tr><td>功能类型：<select id='selPlacePrizeID'>" + InitEventMouldSelect(placePrizeID, TimePropJson) + "</select></td></tr>"
                + "<tr><td>时间：<input type='text' id='txtPlaceTime' value='" + placeTime + "' /></td></tr>"
                + "<tr><td>参数1：<input type='text' id='txtParam1' value='" + param1 + "' /></td></tr>"
                + "<tr><td>参数2：<input type='text' id='txtParam2' value='" + param2 + "' /></td></tr>"
                + "</table>"
    }

    $.dialog({
        title: "修改事件道具参数",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddParmsMould(contr, typeID, prizeID);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

//功能性道具参数设置
function ParamsSetting(contr) {
    var typeID = $(contr).parent().parent().find("lable[name=lblTypeName]").first().attr("id");
    var prizeID = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var prizeName = $(contr).parent().parent().find("lable[name=lblName]").first().text();
    var content = "";
    content = "<input type='button' id='btnAddParams' value='添加参数' onclick='EditParms(\"" + typeID + "\",\"" + prizeID + "\",null)' /><table border='1' id='tblPrizeParms'>";
    //事件道具
    if (typeID == 7) {
        content += "<thead><tr><td>触发事件</td><td>操作</td></tr></thead><tbody>";
    }
    //货币宝箱
    else if (typeID == 8) {
        content += "<thead><tr><td>宝箱类型</td><td>最小值</td><td>最大值</td><td>浮动比</td><td>操作</td></tr></thead><tbody>";
    }
    //道具宝箱
    else if (typeID == 9) {
        
        content += "<thead><tr><td>物品</td><td>概率</td><td>日限</td><td>操作</td></tr></thead><tbody>";
    }
    //限时增强配置
    else if (typeID == 10) {
        content += "<thead><tr><td>时间</td><td>功能类型</td><td>参数1</td><td>参数2</td><td>操作</td></tr></thead><tbody>";
    }

    $.ajax({
        type: "post",
        data: { action: "GetListByPrizeID", prizeID: prizeID },
        dataType: "json",
        url: "../../Ajax/FuncPrizeParmsAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                var json = data.json;
                $.each(json, function (k, v) {

                    content += "<tr id='" + v.ID + "'>"
                    //事件道具
                    if (typeID == 7) {
                        content += "<td><lable name='lblEventMouldName' id='" + v.EventMouldID + "'>";
                        content += v.EventMouldName;
                        content += "</label></td>";
                    }
                    //货币宝箱
                    else if (typeID == 8) {
                        content += "<td><lable name='lblGainPrize' id='" + v.GainPrizeID + "'>";
                        content += v.GainPrizeName;
                        content += "</label></td>";
                        content += "<td><lable name='lblMinValue'>";
                        content += v.MinValue;
                        content += "</label></td>";
                        content += "<td><lable name='lblMaxValue'>";
                        content += v.MaxValue;
                        content += "</label></td>";
                        content += "<td><lable name='lblFloatRatio'>";
                        content += v.FloatRatio;
                        content += "</label></td>";
                    }
                    //道具宝箱
                    else if (typeID == 9) {
                        content += "<td><lable name='lblGainPrize' id='" + v.GainPrizeID + "'>";
                        content += v.GainPrizeName;
                        content += "</label></td>";
                        content += "<td><lable name='lblFuncChance'>";
                        content += v.FuncChance;
                        content += "</label></td>";
                        content += "<td><lable name='lblFuncDayAstrict'>";
                        content += v.FuncDayAstrict;
                        content += "</label></td>";
                    }
                    //限时增强配置
                    else if (typeID == 10) {
                        content += "<td><lable name='lblPlacePrize' id='" + v.PlacePrizeID + "'>";
                        content += v.PlacePrizeName;
                        content += "</label></td>";
                        content += "<td><lable name='lblPlaceTime'>";
                        content += v.PlaceTime;
                        content += "</label></td>";
                        content += "<td><lable name='lblParam1'>";
                        content += v.Param1;
                        content += "</label></td>";
                        content += "<td><lable name='lblParam2'>";
                        content += v.Param2;
                        content += "</label></td>";
                    }
                    content += "<td>";
                    content += "<input type='button' onclick='EditParms(\"" + typeID + "\",\"" + prizeID + "\",this)' value='更新'><input type='button' onclick='DeleteParms(this)' value='删除'>"
                    content += "</td>";
                    content += "</tr>";
                })
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    content += "</tbody></table>";
    $.dialog({
        title: "设置" + prizeName + "参数",
        content: content,
        button: [
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function InitEventMouldSelect(value, json) {
    var sel = "";
    $.each(json, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function InitEventMouldJson() {
    $.ajax({
        type: "post",
        data: { action: "GetListByTypeName" },
        dataType: "json",
        url: "../../Ajax/EventMouldAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                EventMouldJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}

function InitPropJson() {
    $.ajax({
        type: "post",
        data: { action: "GetPropJson" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                PropJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}


function InitTimePropJson() {
    $.ajax({
        type: "post",
        data: { action: "GetTimePropJson" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                TimePropJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}

function InitGoldJson() {
    $.ajax({
        type: "post",
        data: { action: "GetGoldJson" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        async: false,
        success: function (data) {
            if (data != null) {
                GoldJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}

function InitSelect(value) {
    var sel = "";
    $.each(prizeTypeJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function InitExchangeSelect(value) {
    var sel = "";
    $.each(prizeJson, function (k, v) {
        if ((value == null && k == 0) || v.ExchangeID == value)
            sel += "<option value='" + v.ExchangeID + "' selected='selected'>" + v.ExchangeName + "</option>";
        else
            sel += "<option value='" + v.ExchangeID + "'>" + v.ExchangeName + "</option>";
    })
    return sel;
}

function Delete(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var imgSrc = $(contr).parent().parent().find("img[name=imgPath]").first().attr("src");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id, imgPath: imgSrc },
            async: true,
            url: "../../Ajax/PrizeAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(contr).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function Update(contr) {
    Edit(contr);
}

function Edit(contr) {

    $.ajax({
        type: "post",
        data: { action: "GetExchangeTypeJson" },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        async: false,
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                prizeJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var name = $(contr).parent().parent().find("lable[name=lblName]").first().text();
    var typeName = $(contr).parent().parent().find("lable[name=lblTypeName]").first().text();
    var Price = $(contr).parent().parent().find("lable[name=lblPrice]").first().text();
    var remark = $(contr).parent().parent().find("lable[name=lblRemark]").first().text();
    var sortNum = $(contr).parent().parent().find("lable[name=lblSortNum]").first().text();
    var isTaskCondition = $(contr).parent().parent().find("lable[name=lblIsTaskCondition]").first().text();
    var isShopShow = $(contr).parent().parent().find("lable[name=lblIsShopShow]").first().text();
    var typeID = $(contr).parent().parent().find("lable[name=lblTypeName]").first().attr("id");
    var exchangeTypeID = $(contr).parent().parent().find("lable[name=lblExchangeType]").first().attr("id");
    var prizeCount = $(contr).parent().parent().find("lable[name=lblPrizeCount]").first().text();
    content = "";
    if (contr != null) {
        content = "<table><tr><td>物品名称：<input type='text' id='txtName' value='" + name + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>物品类型：<select id='selPrizeType'>" + InitSelect(typeID) + "</select></td></tr>"
        content += "<tr><td>支付物品：<select id='selExchangeType'>" + InitExchangeSelect(exchangeTypeID) + "</select></td></tr>"
        content += "<tr><td>物品价格：<input type='text' id='txtPrice' value='" + Price + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>数量：<input type='text' id='txtCount' value='" + prizeCount + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>描述：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'>" + remark + "</textarea></td></tr>"
        content += "<tr><td>排序号：<input type='text' id='txtSortNum' value='" + sortNum + "' style='width:200px;float:right' /></td></tr>"
        if (isTaskCondition == "True")
            content += "<tr><td>可作为任务条件：<input type='radio' name='radioIsTaskCondition' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' value='False'/><lable>否</lable></td></tr>";
        else
            content += "<tr><td>可作为任务条件：<input type='radio' name='radioIsTaskCondition' value='True' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' value='False' checked='checked'/><lable>否</lable></td></tr>";
        if (isShopShow == "True")
            content += "<tr><td>在商城中显示：<input type='radio' name='radioisShopShow' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioisShopShow' value='False'/><lable>否</lable></td></tr>";
        else
            content += "<tr><td>在商城中显示：<input type='radio' name='radioisShopShow' value='True' /><lable>是</lable><input type='radio' name='radioisShopShow' value='False' checked='checked'/><lable>否</lable></td></tr>";

        content += "<tr><td>上传图片：<iframe id='contentIframe' height='35px' scrolling='no' width='100' frameborder='0' allowtransparency='true' src='../../CommonPage/UpLoadFilePage.aspx' style='margin-top: -10px'></iframe></td></tr></table>";
    }
    else
        content = "<table><tr><td>物品名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>物品类型：<select id='selPrizeType'>" + InitSelect() + "</select></td></tr>"
                + "<tr><td>支付物品：<select id='selExchangeType'>" + InitExchangeSelect() + "</select></td></tr>"
                + "<tr><td>物品价格：<input type='text' id='txtPrice' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>数量：<input type='text' id='txtCount' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>描述：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'></textarea></td></tr>"
                + "<tr><td>排序号：<input type='text' id='txtSortNum' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>可作为任务条件：<input type='radio' name='radioIsTaskCondition' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' value='False'/><lable>否</lable></td></tr>"
                + "<tr><td>在商城中显示：<input type='radio' name='radioisShopShow' value='True' /><lable>是</lable><input type='radio' name='radioisShopShow' value='False' checked='checked'/><lable>否</lable></td></tr>"
                + "<tr><td>上传图片：<iframe id='contentIframe' height='35px' scrolling='no' width='200px' frameborder='0' allowtransparency='true' src='../../CommonPage/UpLoadFilePage.aspx' style='margin-top: -10px'></iframe></td></tr></table>";
    $.dialog({
        title: "更新概率模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objID = id;
                            var objName = $("#txtName").val();
                            if ($.trim(objName) == "") {
                                $.dialog.alert("请输入物品类型名称！");
                                return false;
                            }
                            AddMould(contr, $("#txtName"));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(contr, txtName) {

    var imgPath = $(window.frames["contentIframe"].document).find("#txtPath").val();
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var name = $(txtName).val();
    var price = $(txtName).parent().parent().parent().find("#txtPrice").val();
    var remark = $(txtName).parent().parent().parent().find("#txtRemark").val();
    var sortNum = $(txtName).parent().parent().parent().find("#txtSortNum").val();
    var isTaskCondition = $(txtName).parent().parent().parent().find("input[name=radioIsTaskCondition]:checked").val();
    var isShopShow = $(txtName).parent().parent().parent().find("input[name=radioisShopShow]:checked").val();
    var typeID = $(txtName).parent().parent().parent().find("#selPrizeType").val();
    var typeName = $(txtName).parent().parent().parent().find("#selPrizeType>option:selected").text();
    var oldImgPath = $(contr).parent().parent().find("img[name=imgPath]").first().attr("src");
    var exchangeID = $(txtName).parent().parent().parent().find("#selExchangeType").val();
    var exchangeName = $(txtName).parent().parent().parent().find("#selExchangeType").find("option:selected").text();
    var prizeCount = $(txtName).parent().parent().parent().find("#txtCount").val();

    var data = contr == null ? { action: "Add", name: name, imgPath: imgPath, typeID: typeID, price: price, remark: remark, sortNum: sortNum, isTaskCondition: isTaskCondition, isShopShow: isShopShow, exchangeID: exchangeID, prizeCount: prizeCount} :
              { action: "Update", id: id, name: name, imgPath: imgPath, typeID: typeID, price: price, oldImgPath: oldImgPath, remark: remark, sortNum: sortNum, isTaskCondition: isTaskCondition, isShopShow: isShopShow, exchangeID: exchangeID, prizeCount: prizeCount };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                $.dialog.tips("更新成功！", 1);
                var info = data.split('|'); //id|createTime|imgPath
                if (contr == null) {
                    var tbody = "<tr>";
                    tbody += "<td><lable name='lblName' id='" + info[0] + "'>";
                    tbody += name;
                    tbody += "</label></td>"
                    tbody += "<td><lable name='lblTypeName' id='" + typeID + "'>";
                    tbody += typeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += info[1];
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblExchangeType' id='" + exchangeID + "'>";
                    tbody += exchangeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrice' >";
                    tbody += price;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrizeCount' >";
                    tbody += prizeCount;
                    tbody += "</label></td>"
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += remark;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblSortNum' >";
                    tbody += sortNum;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' >";
                    tbody += isTaskCondition;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsShopShow' >";
                    tbody += isShopShow;
                    tbody += "</label></td>";
                    tbody += "<td><img name='imgPath' src='" + info[2] + "' width='100' height='100'/></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                    $("#tb1").append(tbody);
                }
                else {
                    $(contr).parent().parent().find("lable[name=lblName]").first().text(name);
                    $(contr).parent().parent().find("lable[name=lblTypeName]").first().text(typeName);
                    $(contr).parent().parent().find("lable[name=lblTypeName]").first().attr("id", typeID);
                    $(contr).parent().parent().find("lable[name=lblPrice]").first().text(price);
                    $(contr).parent().parent().find("img[name=imgPath]").first().attr("src", info[2]);
                    $(contr).parent().parent().find("lable[name=lblRemark]").first().text(remark);
                    $(contr).parent().parent().find("lable[name=lblSortNum]").first().text(sortNum);
                    $(contr).parent().parent().find("lable[name=lblIsTaskCondition]").first().text(isTaskCondition);
                    $(contr).parent().parent().find("lable[name=lblIsShopShow]").first().text(isShopShow);
                    $(contr).parent().parent().find("lable[name=lblExchangeType]").first().text(exchangeName);
                    $(contr).parent().parent().find("lable[name=lblExchangeType]").first().attr("id", exchangeID);
                    $(contr).parent().parent().find("lable[name=lblPrizeCount]").first().text(prizeCount);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function AddMouldState(sortId) {
    var data = sortId == null ? {} :
              { action: "Select", state: sortId };
    $.ajax({
        type: "post",
        data: data,
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblTypeName' id='" + v.PrizeTypeID + "'>";
                    tbody += v.TypeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblExchangeType' id='" + v.ExchangeType + "'>";
                    tbody += v.ExchangeTypeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrice' >";
                    tbody += v.Price;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblPrizeCount' >";
                    tbody += v.PrizeCount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += v.Remark;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblSortNum' >";
                    tbody += v.SortNum;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' >";
                    tbody += v.IsTaskCondition;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsShopShow' >";
                    tbody += v.IsShopShow;
                    tbody += "</label></td>";
                    tbody += "<td><img name='imgPath' src='" + v.Image + "' width='100' height='100'/></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}